Skip to content

feat: allow attributes on structure fields#13382

Open
SrGaabriel wants to merge 10 commits intoleanprover:masterfrom
SrGaabriel:master
Open

feat: allow attributes on structure fields#13382
SrGaabriel wants to merge 10 commits intoleanprover:masterfrom
SrGaabriel:master

Conversation

@SrGaabriel
Copy link
Copy Markdown

@SrGaabriel SrGaabriel commented Apr 12, 2026

This PR adds support for attaching attributes directly to structure fields, applying them to the generated projection functions.

Currently, attributes on structure fields are rejected with "Invalid attribute: Attributes cannot be added to fields", requiring users to
write separate attribute commands after the structure definition. This PR removes that restriction and applies field attributes to the corresponding projection declarations during structure elaboration.

Example:

-- Before:
structure Config where
  oldField : Nat
  normalize : String → String

attribute [deprecated "Use newField instead"] Config.oldField
attribute [simp] Config.normalize

-- After:
structure Config where
  @[deprecated "Use newField instead"] oldField : Nat
  @[simp] normalize : String → String

This also enables metadata-style attributes for downstream tooling, such as serialization frameworks:

structure User where
  @[json_rename "user_name"] userName : String
  @[json_rename "email_address"] email : String

The implementation is minimal, it removes the restriction in checkValidFieldModifier and applies attributes to projections in the structure finalization step using the existing Term.applyAttributes mechanism (similar to how docstrings are handled). No new storage or infrastructure was introduced.

Closes #13221

@github-actions github-actions bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Apr 13, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase d76e5a1886956bb38dc6bd2868260550dc66c309 --onto d53b46a0f3a7d0822550f4179ca3dab446a3c448. You can force Mathlib CI using the force-mathlib-ci label. (2026-04-13 00:25:37)

@leanprover-bot
Copy link
Copy Markdown
Collaborator

Reference manual CI status:

  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase d76e5a1886956bb38dc6bd2868260550dc66c309 --onto 861bc19e0c1c45b5766cc5c9ef0488568368b236. You can force reference manual CI using the force-manual-ci label. (2026-04-13 00:25:38)

@SrGaabriel
Copy link
Copy Markdown
Author

SrGaabriel commented Apr 14, 2026

Another great advantage I just noticed: currently you have to derive separately after applying attributes if you want the derive to spot them:

structure Custom where
  type_ : String
  deriving BEq, Repr

attribute [serial_name "type"] Custom.type_

deriving instance Serialize for Custom
deriving instance Deserialize for Custom

With this PR, you can just do

structure Custom where
  @[serial_name "type"]
  type_ : String
  deriving BEq, Repr, Serialize, Deserialize

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RFC: Field attributes

2 participants